package com.ruoyi.web.controller.market.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.db.domain.BaseTypeInfo;
import com.ruoyi.db.domain.ProductInfo;
import com.ruoyi.db.domain.SpecialCostomItemInfo;
import com.ruoyi.extend.enums.ChannelTypeEnum;
import com.ruoyi.market.enums.SpecialEnum;
import com.ruoyi.product.constant.ClientTypeConstant;
import com.ruoyi.product.enums.NoShowTypeEnum;
import com.ruoyi.product.service.IProductInfoService;
import com.ruoyi.product.service.ISceneInfoService;
import com.ruoyi.sys.service.IAddressInfoService;
import com.ruoyi.web.wapper.ProductPushWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.ProductPush;
import com.ruoyi.product.service.IProductPushService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品上架管理Controller
 *
 * @author ruoyi
 * @date 2022-07-31
 */
@Controller
@RequestMapping("/product/push")
public class ProductPushController extends BaseController {
    private String prefix = "product/push";

    @Autowired
    private IProductPushService productPushService;
    @Autowired
    private IProductInfoService productInfoService;
    @Autowired
    private ISceneInfoService sceneInfoService;
    @Autowired
    private IAddressInfoService addressInfoService;


    @RequiresPermissions("product:push:view")
    @GetMapping()
    public String push(ModelMap mmap) {

        // 产品信息
        List<Map<String, Object>> productMapList = productInfoService.listMap(new ProductInfo());
        mmap.put("productList", productMapList);


        List<Map<String, Object>> channelTypeList = ChannelTypeEnum.getShowMap();
        mmap.put("channelTypeList", channelTypeList);

        // 不展示的量列表
        List<Map<String, Object>> noShowTypeList = NoShowTypeEnum.getShowMap();
        mmap.put("noShowTypeList", noShowTypeList);

        return prefix + "/push";
    }

    /**
     * 查询商品上架管理列表
     */
    @RequiresPermissions("product:push:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductPush productPush) {

        // 场景列表
        List<Map<String, Object>> scencInfoList = sceneInfoService.listMap();

        startPage();
        List<Map<String, Object>> mapList = productPushService.selectProductPushListMap(productPush);
        mapList = new ProductPushWapper(mapList).setScencInfoList(scencInfoList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:push:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ProductPush productPush) {
        boolean result = productPushService.changeStatus(productPush);
        return toAjax(result);
    }

    /**
     * 导出商品上架管理列表
     */
    @RequiresPermissions("product:push:export")
    @Log(title = "商品上架管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductPush productPush) {
        List<ProductPush> list = productPushService.selectProductPushList(productPush);
        ExcelUtil<ProductPush> util = new ExcelUtil<ProductPush>(ProductPush.class);
        return util.exportExcel(list, "商品上架管理数据");
    }

    /**
     * 新增商品上架管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        // 产品列表
        // 选择没有上架过的产品
        List<Map<String, Object>> productInfoList = productInfoService.listMapPush();
        mmap.put("productInfoList", productInfoList);

        // 场景列表
        List<Map<String, Object>> scencInfoList = sceneInfoService.listMap();
        mmap.put("scencInfoList", scencInfoList);

        // 客户端列表
        List<Map<String, Object>> clietTypeList = ClientTypeConstant.mapList;
        mmap.put("clietTypeList", clietTypeList);

        // 不展示的量列表
        List<Map<String, Object>> noShowTypeList = NoShowTypeEnum.getShowMap();
        mmap.put("noShowTypeList", noShowTypeList);


        List<Map<String, Object>> channelTypeList = ChannelTypeEnum.getShowMap();
        mmap.put("channelTypeList", channelTypeList);

        List<Map<String, String>> zhiMaTypeList = SpecialEnum.ZhimaScore.getDataMap();
        mmap.put("zhiMaTypeList", zhiMaTypeList);

        return prefix + "/add";
    }

    /**
     * 新增保存商品上架管理
     */
    @RequiresPermissions("product:push:add")
    @Log(title = "商品上架管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductPush productPush) {
        return toAjax(productPushService.insertProductPush(productPush));
    }

    /**
     * 修改商品上架管理
     */
    @RequiresPermissions("product:push:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {

        ProductPush productPush = productPushService.selectProductPushById(id);
        mmap.put("productPush", productPush);

        // 场景列表
        List<Map<String, Object>> scencInfoList = sceneInfoService.listMap();
        for (Map<String, Object> map : scencInfoList) {
            List<Long> jsonArray = JSON.parseArray(productPush.getSceneIds(), Long.class);
            Long value = (Long) map.get("value");
            if (jsonArray.contains(value)) {
                map.put("flag", true);
            } else {
                map.put("flag", false);
            }
        }
        mmap.put("scencInfoList", scencInfoList);

        // 客户端列表
        List<Map<String, Object>> clientTypeList = ClientTypeConstant.mapList;
        for (Map<String, Object> map : clientTypeList) {
            JSONArray jsonArray = JSON.parseArray(productPush.getClientTypes());
            String value = (String) map.get("value");
            if (jsonArray.contains(value)) {
                map.put("flag", true);
            } else {
                map.put("flag", false);
            }
        }
        mmap.put("clientTypeList", clientTypeList);

        List<Map<String, Object>> channelTypeList = ChannelTypeEnum.getShowMap();
        for (Map<String, Object> map : channelTypeList) {
            JSONArray jsonArray = JSON.parseArray(productPush.getChannelTypes());
            String value = (String) map.get("value");
            if (jsonArray.contains(value)) {
                map.put("flag", true);
            } else {
                map.put("flag", false);
            }
        }
        mmap.put("channelTypeList", channelTypeList);

        List<Map<String, Object>> noShowTypeList = NoShowTypeEnum.getShowMap();
        List<String> noShowList = JSON.parseArray(productPush.getNotExcludeBrowser(), String.class);
        for (Map<String, Object> map : noShowTypeList) {
            String value = (String) map.get("value");
            if (CollUtil.contains(noShowList, value)) {
                map.put("flag", String.valueOf(true));
            } else {
                map.put("flag", String.valueOf(false));
            }
        }
        mmap.put("noShowTypeList", noShowTypeList);

        List<Map<String, String>> zhiMaTypeList = SpecialEnum.ZhimaScore.getDataMap();
        List<String> zhimaFilterList = JSON.parseArray(productPush.getZhimaFilter(), String.class);
        for (Map<String, String> map : zhiMaTypeList) {
            String value = map.get("value");
            if (CollUtil.contains(zhimaFilterList, value)) {
                map.put("flag", String.valueOf(true));
            } else {
                map.put("flag", String.valueOf(false));
            }
        }
        mmap.put("zhiMaTypeList", zhiMaTypeList);



        return prefix + "/edit";


    }

    /**
     * 修改保存商品上架管理
     */
    @RequiresPermissions("product:push:edit")
    @Log(title = "商品上架管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductPush productPush) {
        return toAjax(productPushService.updateProductPush(productPush));
    }

    /**
     * 删除商品上架管理
     */
    @RequiresPermissions("product:push:remove")
    @Log(title = "商品上架管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productPushService.deleteProductPushByIds(ids));
    }

    /**
     * 加载地址管理树列表
     */
    @GetMapping("/noCity")
    @ResponseBody
    public List<Ztree> noCity(@RequestParam("id") String id) {
        ProductPush productPush =
                productPushService.selectProductPushById(Long.valueOf(id));

        List<String> noCityList = JSON.parseArray(productPush.getNotShowCitys(), String.class);
        List<Ztree> ztrees = addressInfoService.selectAddressInfoTreeOfChoose(noCityList);
        return ztrees;
    }


}
